package net.destroyer.game.tetris.core;

import java.awt.Component;

import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Vector3f;

import net.destroyer.game.tetris.core.gamespace.GameSpace;
import net.destroyer.game.tetris.core.shape.GameCubes;
import net.destroyer.game.tetris.core.shape.TGameCube;
import net.destroyer.game.tetris.core.util.Origin;
import net.destroyer.game.tetris.core.util.StartPoint;

import com.sun.j3d.utils.universe.SimpleUniverse;

public class Engine implements IEngine {
	protected SimpleUniverse univ;
	protected Canvas3D can;

	public Engine() {
		can = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
		univ = new SimpleUniverse(can);
		univ.getViewingPlatform().setNominalViewingTransform();
		can.getView().setMinimumFrameCycleTime(33);
	    BranchGroup scene  = new BranchGroup();

	    
	    
	    TransformGroup transformGroup = new TransformGroup();
	  Transform3D tr1 = new Transform3D();
	 
	  Transform3D tr2 = new Transform3D();
	  //tr2.rotY(Math.PI/6);
	  tr2.setTranslation(new Vector3f(0,0,-15));
	  tr2.mul(tr1);
	  Transform3D tr3 = new Transform3D();
	  tr3.rotX(Math.PI/6);
	  tr3.mul(tr2);
	  transformGroup.setTransform(tr2);
	  scene.addChild(transformGroup);
	  
	  TransformGroup tgspace = new TransformGroup();
	  Transform3D ff = new Transform3D();
	  ff.setIdentity();
	  ff.setTranslation(new Vector3f(0f,0f,-0.5f));
	  tgspace.setTransform(ff);
	  tgspace.addChild(new GameSpace(5, 10));
	  tgspace.addChild(new Origin());

	  transformGroup.addChild(tgspace);
	  // Make a changeable 3D transform
	  TransformGroup trans = new TransformGroup();
	  trans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
	  trans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
	  transformGroup.addChild(trans);
	  GameCubes s = new TGameCube(new StartPoint(9));
	  System.out.println(s.getRefPoints());
	  transformGroup.addChild(s);
	 

	  
	    scene.compile();
	    univ.addBranchGraph(scene);
	}

	@Override
	public Component getVisualResult() {
		return can;
	}
}
